package edu.unl.bsm.CoolingLoad.Util;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;

import org.joda.time.DateTime;

import jxl.read.biff.BiffException;

import edu.unl.bsm.CoolingLoad.Data.IData;
import edu.unl.bsm.CoolingLoad.Data.WeatherData;

/**
 * 
 * @author ZhongYin Zhang
 *
 */
public class LastNHour {
	
	
	/**
	 * Get last N hour data 
	 * @param localTime the local time in database
	 * @param n   n the parameter in ARX(1,2,24) || ARX(24,168);
	 * @return
	 * @throws BiffException
	 * @throws IOException
	 * @throws ParseException
	 */
	public static IData getLastNHour(Date localTime, int n, IData noaaData) throws BiffException, IOException, ParseException{
		
		IData lastHourData = new WeatherData();
		
		HashMap<Date, Float> T_t = noaaData.getT_t();
		HashMap<Date, Integer> dayOfWeek = noaaData.getDayofWeek();
		HashMap<Date, Integer> hourOfDay = noaaData.getHour();
		HashMap<Date, Float> T_MAX_t = noaaData.getT_MAX_t();
		HashMap<Date, Float> T_MIN_t = noaaData.getT_MIN_t();
		HashMap<Date, Float> RH_t = noaaData.getRH_t();
		HashMap<Date, Float> CoolLoad_t = noaaData.getCoolLoad_t();
		HashMap<Date, String> time = noaaData.getLocalTime();
		
		
		
		
		Date lt = (Date) localTime.clone();
		//Monday to Friday 
		DateTime dt = new DateTime(lt);
		DateTime lastNhour = dt.minusHours(n);
		
		Timestamp date = Utility.converToDateHH(lastNhour.toDate()); 
//		date.setHours(date.getHours()-n);
			
//			if(date.getDay() == 0){
//				date.setHours(date.getHours()-48);
//			}
//			System.out.println(date);
			//last hour T_Max_t
			HashMap<Date, Float> lastHour_T_MAX_t = new HashMap<Date, Float>();
			lastHour_T_MAX_t.put(date, T_MAX_t.get(date));	
			lastHourData.setT_MAX_t(lastHour_T_MAX_t);
			lastHourData.setTmaxt(T_MAX_t.get(date));
			
			//last hour T_Min_t
			HashMap<Date, Float> lastHour_T_MIN_t = new HashMap<Date, Float>();
			lastHour_T_MIN_t.put(date, T_MIN_t.get(date));	
			lastHourData.setT_MIN_t(lastHour_T_MIN_t);
			lastHourData.setTmint(T_MIN_t.get(date));
			
			//last hour RH_t
			HashMap<Date, Float> lastHour_RH_t = new HashMap<Date, Float>();
			lastHour_RH_t.put(date, RH_t.get(date));	
			lastHourData.setRH_t(lastHour_RH_t);
			lastHourData.setRHt(RH_t.get(date));
			
			//last hour T_t
			HashMap<Date, Float> lastHour_T_t = new HashMap<Date, Float>();
			lastHour_T_t.put(date, T_t.get(date));
			lastHourData.setT_t(lastHour_T_t);
			lastHourData.setTt(T_t.get(date));
			
			//last hour Cooling Load
			HashMap<Date, Float> lastHour_CoolLoad_t = new HashMap<Date, Float>();
			lastHour_CoolLoad_t.put(date, CoolLoad_t.get(date));
			lastHourData.setCoolLoad_t(lastHour_CoolLoad_t);
			lastHourData.setCoolLoadt(CoolLoad_t.get(date));
			
			//last hour day of week
			HashMap<Date, Integer> lastHour_dayOfWeek = new HashMap<Date, Integer>();
			lastHour_dayOfWeek.put(date, dayOfWeek.get(date));
			lastHourData.setDayofWeek(lastHour_dayOfWeek);
			
			//last hour 
			HashMap<Date, Integer> lastHour_hourOfDay = new HashMap<Date, Integer>();
			lastHour_hourOfDay.put(date, hourOfDay.get(date));
			lastHourData.setHour(lastHour_hourOfDay);
			
			//last hour local time
			HashMap<Date, String> lastHour_localTime = new HashMap<Date, String>();
			lastHour_localTime.put(date, time.get(date));			
			lastHourData.setLocalTime(lastHour_localTime);
	
		
		
		return lastHourData;
	}
	
	
	public void toString(IData data){
		System.out.println("Local Time"+data.getLocalTime().toString());
		System.out.println("Day of the week"+data.getDayofWeek().toString());
		System.out.println("hour"+ data.getHour().toString());
		System.out.println("Max t"+ data.getT_MAX_t().toString());
		System.out.println("Min t"+ data.getT_MIN_t().toString());
		System.out.println("T_t" + data.getT_t().toString());
		System.out.println("RH_t"+ data.getRH_t().toString());
		System.out.println("Cool Load t"+data.getCoolLoad_t().toString());
		
		System.out.println("Max t"+ data.getTmaxt());
		System.out.println("Min t"+ data.getTmint());
		System.out.println("T_t" + data.getTt());
		System.out.println("RH_t"+ data.getRHt());
		System.out.println("Cool Load t"+data.getCoolLoadt());
		
	}

}
